package czk.concrent.threadpool.timer;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Timer单线程调度演示
 * @Author:ChenZhangKun
 * @Date: 2020/7/8 22:17
 */
public class TimerDemo {
    public static void main(String[] args) {
        // 创建timer
        Timer timer=new Timer();
        // 创建两个任务
        TimerTask task1=new TimerTask() {
            @Override
            public void run() {
                System.out.println("1");
                try {
                    Thread.sleep(2*1000);
                } catch (InterruptedException e) {

                }
            }
        };
        TimerTask task2=new TimerTask() {
            @Override
            public void run() {
                System.out.println(2);
            }
        };
        timer.schedule(task1,1000);
        // 会延迟执行，因为timer是单线程调度
        timer.schedule(task2,1000);
    }
}
